Socket
Socket
Sign inDemoInstall

ware

Package Overview
Dependencies
1
Maintainers
3
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

ware


Version published
Maintainers
3
Created

Package description

What is ware?

The 'ware' npm package is a middleware engine for handling a series of functions in a pipeline. It allows you to compose middleware functions and execute them in sequence, making it useful for tasks such as request handling, data processing, and more.

What are ware's main functionalities?

Middleware Composition

This feature allows you to compose multiple middleware functions and execute them in sequence. Each middleware function can modify the request and response objects and pass control to the next function in the pipeline.

const ware = require('ware');

const middleware = ware()
  .use((req, res, next) => {
    req.processed = true;
    next();
  })
  .use((req, res, next) => {
    res.message = 'Hello, world!';
    next();
  });

const req = {};
const res = {};

middleware.run(req, res, (err) => {
  if (err) throw err;
  console.log(req.processed); // true
  console.log(res.message); // 'Hello, world!'
});

Error Handling

This feature demonstrates how 'ware' handles errors in the middleware pipeline. If an error is passed to the 'next' function, the subsequent middleware functions are skipped, and the error is handled in the final callback.

const ware = require('ware');

const middleware = ware()
  .use((req, res, next) => {
    next(new Error('Something went wrong'));
  })
  .use((req, res, next) => {
    res.message = 'This will not be executed';
    next();
  });

const req = {};
const res = {};

middleware.run(req, res, (err) => {
  if (err) {
    console.error(err.message); // 'Something went wrong'
  }
});

Other packages similar to ware

Readme

Source

ware

Easily create your own middleware layer.

Build Status

Installation

Node:

$ npm install ware

Component:

$ component install segmentio/ware

Duo:

var ware = require('segmentio/ware');

Example

var ware = require('ware');
var middleware = ware()
  .use(function (req, res, next) {
    res.x = 'hello';
    next();
  })
  .use(function (req, res, next) {
    res.y = 'world';
    next();
  });

middleware.run({}, {}, function (err, req, res) {
  res.x; // "hello"
  res.y; // "world"
});

Give it any number of arguments:

var ware = require('ware');
var middleware = ware()
  .use(function (a, b, c, next) {
    console.log(a, b, c);
    next();
  })

middleware.run(1, 2, 3); // 1, 2, 3

Supports generators (on the server):

var ware = require('ware');
var middleware = ware()
  .use(function (obj) {
    obj.url = 'http://google.com';
  })
  .use(function *(obj) {
    obj.src = yield http.get(obj.url);
  })

middleware.run({ url: 'http://facebook.com' }, function(err, obj) {
  if (err) throw err;
  obj.src // "obj.url" source
});

API

ware()

Create a new list of middleware.

.use(fn)

Push a middleware fn onto the list. fn can be a synchronous, asynchronous, or generator function. fn can also be an array of functions or an instance of Ware.

.run(input..., [callback])

Runs the middleware functions with input... and optionally calls callback(err, input...).

License

(The MIT License)

Copyright (c) 2013 Segment.io <friends@segment.io>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Last updated on 01 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc